import 'package:flutter/material.dart';

class TextButtonWithIcon extends TextButton {
  TextButtonWithIcon({
    Key? key,
    required VoidCallback? onPressed,
    VoidCallback? onLongPress,
    ButtonStyle? style,
    FocusNode? focusNode,
    bool? autofocus,
    Clip? clipBehavior,
    IconTextAlignment? iconTextAlignment,
    double? textIconPadding,
    required Widget icon,
    required Widget label,
  }) : super(
          key: key,
          onPressed: onPressed,
          onLongPress: onLongPress,
          style: style,
          focusNode: focusNode,
          autofocus: autofocus ?? false,
          clipBehavior: clipBehavior ?? Clip.none,
          child: _TextButtonWithIconChild(
            icon: icon,
            label: label,
            iconTextAlignment: iconTextAlignment,
            textIconPadding: textIconPadding,
          ),
        );
}

class _TextButtonWithIconChild extends StatelessWidget {
  final Widget label;
  final Widget icon;
  final IconTextAlignment? iconTextAlignment;
  final double? textIconPadding;

  const _TextButtonWithIconChild({
    Key? key,
    required this.label,
    required this.icon,
    this.iconTextAlignment = IconTextAlignment.iconLeftTextRight,
    this.textIconPadding = 0,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    switch (iconTextAlignment) {
      case IconTextAlignment.iconTopTextBottom:
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[icon, SizedBox(width: textIconPadding), Flexible(child: label)],
        );
      case IconTextAlignment.iconBottomTextTop:
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[Flexible(child: label), SizedBox(width: textIconPadding), icon],
        );
      case IconTextAlignment.iconLeftTextRight:
        return Row(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[icon, SizedBox(width: textIconPadding), Flexible(child: label)],
        );
      case IconTextAlignment.iconRightTextLeft:
        return Row(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[Flexible(child: label), SizedBox(width: textIconPadding), icon],
        );
      default:
        return Row(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[icon, SizedBox(width: textIconPadding), Flexible(child: label)],
        );
    }
  }
}

enum IconTextAlignment {
  iconTopTextBottom, //图在上文字在下
  iconBottomTextTop, //图在下文字在上
  iconLeftTextRight, //图在左文字在右
  iconRightTextLeft, //图在右文字在左
}
